Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Once you have set the font and colors, set the Text Box’s Text property to a blank string. You’ll find it helpful to leave the default text displayed while you are choosing your color schemes—that way, you can see the effect of your various attempts. You may also need to increase the height of the Text Box to accommodate the larger font setting. #### Creating The Control Array Of Number Buttons The next step in creating the calculator’s interface is to place the 10 Command Buttons for entering the digits 0 through 9. We will use a new technique called a control array. Once you’ve seen how this works, I think you’ll agree that it is a powerful tool. When you place individual controls on a form, each one has its own name and its own event procedures. In contrast, a control array contains two or more controls of the same type with the same name and the same event procedures. Each control in an array still has its own set of properties, however. In the event procedure, Visual Basic provides an Index argument that specifies which of the controls in the array received the event. As a result, you can write a single event procedure that handles events to two or more controls. This concept will be easier to understand if you try it. Start by placing a Command Button on the form in the position where you want the “0” button (in the lower-left corner, if you’re following my design). Change the Name property of this button to cmdNumbers. With the new Command Button selected, press Ctrl+C to copy the control to the Clipboard, then press Ctrl+V to paste it on the form. The form—and not a control—must be selected for you to use the Paste command. Visual Basic displays a dialog box asking if you want to create a control array. Select Yes. The duplicate Command Button will be inserted in the top-left corner of the form. Drag it to the location where you want the “1” button to appear. Before adding the remaining buttons, let’s briefly look at the properties of the Command Button that was just added. Its name is cmdNumbers, the same as the original button that you copied. Its Index property, however, is 1. If you select the original Command Button, you’ll see that its Index property is 0. This is how Visual Basic distinguishes between controls in a control array. Now you can finish inserting the eight remaining number buttons. For each one, simply press Ctrl+V to paste another copy of the button from the Clipboard, and drag it to the desired location. You should do this in numerical order, beginning with the “2” button. You’ll soon see the reason for this. Once all 10 number buttons have been added to the form, go through and select each one, changing its Caption property to the proper number—that is, the same as its Index property. When you’re finished, your form will look something like Figure 5.3. Don’t forget to save your project. I used the name Calculator for both the form and the project. Figure 5.3 The Calculator form after adding the number buttons and the Text Box for display. #### Creating The Array Of Operator Buttons The calculator also needs four buttons for the basic arithmetic operations: addition, subtraction, multiplication, and division. We will use a control array for these buttons as well. Place a new Command Button on the form, and change its Name property to cmdOperators. Then use the copy and paste techniques from the previous section to create a control array of four buttons. Change the buttons’ Caption properties, as shown in Table 5.2. #### Changing The Button Font Visual Basic’s default font is a bit too small for the Command Button captions. Rather than changing the Font property of each button individually—a tedious task—you can change a property for multiple controls at one time. First, select all the controls. The controls do not have to be the same type (although, in this case, they will all be Command Buttons). You can either use the Shift+Click method or select the Pointer tool and use the mouse to drag an outline (marquee) around the desired controls. When you release the mouse button, all controls that are completely within the rectangle will be selected. To deselect an individual control while leaving the others selected, press Shift and click on it. Table 5.2 Caption property settings for the array of Command Buttons.
Button Index | Caption
--- | ---
0 | +
1 | -
2 | *
3 | /
When you select multiple controls, the Properties window displays only those properties that all of the controls have in common. For example, if you selected a Text Box and a Command Button, you would not see the Text property displayed in the Properties window—only the Text Box control has this property. BackColor and Font (among others) are two properties these two control types have in common, so these properties would display in the Properties window. When you make a change to a common property, the change is reflected in all selected controls. Now use this technique to change the Font property for all the Command Buttons you have placed on the form. Experiment to find a font that looks good to you. I used MS Sans Serif, 18 point. #### Adding The Other Calculator Buttons Any calculator will need a few more buttons: Clear to erase the display, Backspace to delete the last number entered, a Decimal button, and a +/- button to change the sign of the number in the display. Don’t forget the Enter button for placing values on the stack. Go ahead and add these five buttons, making the Enter button bigger than all the rest. Note that these are individual buttons, not a control array. Set the properties as shown in Table 5.3. You’ll probably want to change the Font property for these new buttons also. When you are finished, your form will look more or less like Figure 5.4. #### The Number Button Event Procedures The visual interface is complete—at least for now. We can begin writing the code that will give the calculator its “smarts.” A calculator’s most basic task is to display numbers as the user clicks on the buttons, so we’ll start with that. Double-click on one of the number buttons on the form to display the skeleton of the Click event procedure. The first line looks like this:
    Private Sub cmdNumbers_Click(Index As Integer)
Table 5.3 Name property settings for the additional Command Buttons.
Button Caption | Name Property
--- | ---
Enter | cmdEnter
+/- | cmdPlusMinus
Clear | cmdClear
BS | cmdBackspace
. | cmdDecimal
Figure 5.4 The Calculator form after adding the remaining buttons.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.